# -*- coding: utf-8 -*-

__author__ = 'bilibili'

import types
import copy
from spot_excel_reader import spot_excel_reader
from spot_config import spot_config
from spot_excel_analyzer_base import spot_excel_analyzer_base

class spot_excel_sum_analyzer(spot_excel_analyzer_base):
    # FUNCTION: __init__(self, string)
    #   the constructor of the class spot_order_funnal_analyzer
    # ARGS:
    #   @config_name: json config file name
    def __init__(self, config_name):
        spot_excel_analyzer_base.__init__(self, config_name)

    def fetch_key(self, in_dict):
        return in_dict['THEATER'] + '~' + in_dict['AREA']

    def new_value(self, in_dict):
        attr_list = self.config.get_attr('config', 'sum_columns')
        tmp_dict = copy.deepcopy(in_dict)
        for i in attr_list:
            tmp_dict[i] = 0
        return tmp_dict

    def value_operation(self, key, in_dict):
        attr_list = self.config.get_attr('config', 'sum_columns')
        for i in attr_list:
            if in_dict[i] != '':
                self.res_dict[key][i] += float(in_dict[i])

    def post_build_operation(self):
        return
        tmp_dict = {}
        for d in self.res_dict.keys():
            for i in range(0, len(d)):
                if d[i] == '~':
                    split_index = i
            theater_name = d[0: split_index]
            area_name = d[split_index + 1: len(d)]
            if not tmp_dict.has_key(theater_name):
                tmp_dict[theater_name] = []
            tmp_dict[theater_name].append(self.res_dict[d])
        self.res_dict = tmp_dict

    def customized_calculation(self, arg_list):
        pass

class spot_plan_analyzer(spot_excel_analyzer_base):
    # FUNCTION: __init__(self, string)
    #   the constructor of the class spot_order_funnal_analyzer
    # ARGS:
    #   @config_name: json config file name
    def __init__(self, config_name):
        spot_excel_analyzer_base.__init__(self, config_name)

    def fetch_key(self, in_dict):
        return in_dict['Theatre'] + '~' + in_dict['Area']

    def new_value(self, in_dict):
        tmp_dict = copy.deepcopy(in_dict)
        del tmp_dict['Bookings Quarter']
        return tmp_dict

    def value_operation(self, key, in_dict):
        booking_dict = in_dict['Bookings Quarter']
        year_quarter = booking_dict['Year Quarter']
        del booking_dict['Year Quarter']
        # if self.res_dict[key].has_key(year_quarter):
        #     for i_key in booking_dict.keys():
        #         self.res_dict[key][year_quarter][i_key] += booking_dict[i]
        # else:
        self.res_dict[key][year_quarter] = booking_dict

    def post_build_operation(self):
        tmp_dict = {}
        for d in self.res_dict.keys():
            for i in range(0, len(d)):
                if d[i] == '~':
                    split_index = i
            theater_name = d[0: split_index]
            area_name = d[split_index + 1: len(d)]
            if not tmp_dict.has_key(theater_name):
                tmp_dict[theater_name] = []
            tmp_dict[theater_name].append(self.res_dict[d])
        self.res_dict = tmp_dict

    def customized_calculation(self, arg_list):
        pass

class spot_theater_BCPR_analyzer:
    def __init__(self, ASL_config_name, area_config_name, PLAN_config_name):
        self.asl_analyzer = spot_excel_sum_analyzer(ASL_config_name)
        self.area_analyzer = spot_excel_sum_analyzer(area_config_name)
        self.plan_analyzer = spot_plan_analyzer(PLAN_config_name)
        self.res_dict = {}

    def initialize(self):
        self.asl_analyzer.build_whole_table()
        self.area_analyzer.build_whole_table()
        self.plan_analyzer.build_whole_table()

    # Theater, Year, Quarter
    # def combine_dicts(self):
    #     for key in self.asl_analyzer.keys():
    #         self.res_dict[key][]

def test_spot_excel_sum_analyzer():
    analyzer = spot_excel_sum_analyzer('C:\\svn_repo\\SPOT\\trunk\\DashboardDataService\\resources\\area.config.json')
    analyzer.build_whole_table()
    print analyzer.get_result_dict()

def test_spot_plan_analyzer():
    analyzer = spot_plan_analyzer('C:\\svn_repo\\SPOT\\trunk\\DashboardDataService\\resources\\PLAN new.config.json')
    analyzer.build_whole_table()
    print analyzer.get_result_dict()

if __name__ == '__main__':
    test_spot_excel_sum_analyzer()
    # test_spot_plan_analyzer()